#ifndef _ASYNCTCPSOCKIMPL_H_
#define _ASYNCTCPSOCKIMPL_H_

#include "stdafx.h"

class CTcpSocketImpl :		public	ITcpSocket,
							public	CUnknownImp,
							public  IoStreamBase
{
public:
	CTcpSocketImpl(void);
	virtual ~CTcpSocketImpl(void);
	
	BEGIN_STDCOM_MAP
		STDCOM_INTERFACE_ENTRY(ITcpSocket)
		STDCOM_INTERFACE_ENTRY_UNKNOWN_(ITcpSocket)
	END_STDCOM_MAP

	//IIoDevice
	std_method(CloseIo)(UINT rw);
	std_method(BindIo)();
	std_method(OpenIo)();

	//ISocket
	std_method(CreateSock)();
	std_method(CloseSock)();
	std_method(IsOpen)();
	std_method(AttachSock)(_sock_t sock);
	std_method_(_sock_t, DetachSock)();
	std_method(SetOption)(LPCSTR opt, int val);
	std_method(GetOption)(LPCSTR opt, int* val);
	
	//ITcpSocket
	std_method_(BUFFER_SIZE, SyncSend)(BUFFER_PTR buf, BUFFER_SIZE size);
	std_method_(BUFFER_SIZE, SyncRecv)(BUFFER_PTR buf, BUFFER_SIZE size);
	std_method(SyncConnect)(NET_ADDR addr, NET_PORT port);
	std_method(ReadIo)(IBase* pBase, event_id event);
	std_method(WriteIo)(IBase* pBase, event_id event);
	std_method(ConnectIo)(NET_ADDR addr, NET_PORT port,
		IBase* pBase, event_id event);

private:
	typedef map<string, int>			SocketOptionTable;
	typedef SocketOptionTable::iterator	SocketOption;

	_sock_t				m_sock;
	ULONG				m_ulRecvBufLen;
	ULONG				m_ulSendBufLen;
	int					m_iConTmSec;
	SocketOptionTable	m_MapSockOption;
private:
	void				ConnectCB(void *data, event_id error_code);
	void				StreamWritecb(void *data, BUFFER_SIZE len);
	void				StreamReadcb(void *data, BUFFER_SIZE len);
	void				StreamEventcb(void *data, event_id what);

public:
	IoTcpBase*		GetIoTcp();
};

#endif
